/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.menosehmais.controller.rest;

import br.com.caelum.vraptor.Delete;
import static br.com.caelum.vraptor.view.Results.json;

import br.com.caelum.vraptor.Get;
import br.com.caelum.vraptor.Validator;
import javax.servlet.http.HttpServletRequest;
import br.com.caelum.vraptor.Path;
import br.com.caelum.vraptor.Post;
import br.com.caelum.vraptor.Put;
import br.com.caelum.vraptor.Resource;
import br.com.caelum.vraptor.Result;
import br.com.menosehmais.business.PortletBO;
import br.com.menosehmais.dto.PortletDTO;
import br.com.menosehmais.framework.loggin.StackTraceLogger;
import br.com.menosehmais.framework.persistence.DomainObject;
import br.com.menosehmais.framework.persistence.Id;
import br.com.menosehmais.framework.presentation.AbstractRestController;
import br.com.menosehmais.model.PortletDO;

/**
 *
 * @author junior
 */
@Resource
public class ManterPortletsController extends AbstractRestController {
    
    private final PortletBO portletBO;
    
    public ManterPortletsController(HttpServletRequest request, Result result, Validator validator, PortletBO portletBO) {
        super(request, result, validator);
        this.portletBO = portletBO;
    }
    
    @Post
    @Path("/rest/portlet")
    public void create (PortletDTO portletDTO) {
        validator.validate(portletDTO);
        if(validator.hasErrors()){
            validator.onErrorForwardTo(this.<DomainObject>getRestResponseControllerClass()).error();
        }
        PortletDO portletDO = portletDTO.toDomainObject();
        DomainObject domainObject = portletBO.insert(portletDO);
        
        if(domainObject != null) {
            result.use(json()).withoutRoot().from(domainObject).serialize();
            //result.forwardTo(this.<DomainObject>getRestResponseControllerClass()).success(domainObject);
        } else {
            result.forwardTo(this.<DomainObject>getRestResponseControllerClass()).error();
        }
    }
    
    @Put
    @Path("/rest/portlet/{portletDTO.id:" + Id.REGEX + "}")
    public void update (PortletDTO portletDTO) {
        PortletDO portletDO = portletDTO.toDomainObject();
        DomainObject domainObject = portletBO.update(portletDO);
        if(domainObject != null) {
            //result.forwardTo(this.<DomainObject>getRestResponseControllerClass()).success();
            result.use(json()).withoutRoot().from(true).serialize();
        } else {
            result.forwardTo(this.<DomainObject>getRestResponseControllerClass()).error();
        }
        
    }
    
    @Get
    @Path("/rest/portlet/{id}")
    public void show (String id) {
        PortletDO portletDO = portletBO.selectById(id);
        if(portletDO != null) {
            result.use(json()).withoutRoot().from(portletDO).serialize();
        } else {
            result.forwardTo(this.<PortletDO>getRestResponseControllerClass()).error();
        }
    }
    
    @Delete
    @Path("/rest/portlet/{id}")
    public void destroy (String id) {
        PortletDO portletDO = portletBO.selectById(id);
        if(portletDO != null) {
            try {
                portletBO.delete(portletDO);
                result.use(json()).withoutRoot().from(true).serialize();
            } catch (Exception e) {
                StackTraceLogger.INSTANCE.printStackTrace(e);
                result.forwardTo(this.<PortletDO>getRestResponseControllerClass()).error();
            }
        } else {
            result.forwardTo(this.<PortletDO>getRestResponseControllerClass()).error();
        }
    }
    
    @Get
    @Path("/rest/portlets")
    public void list () {
        //Pegar quem está logado
        //Retornar lista de portlets do usuário logado
    }
}
